package com.wangkang.ucenter.service.impl;
import com.wangkang.commonutils.JwtUtils;
import com.wangkang.commonutils.MD5;
import com.wangkang.commonutils.result.ResultCode;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import com.wangkang.ucenter.entity.Member;
import com.wangkang.ucenter.mapper.MemberMapper;
import com.wangkang.ucenter.model.form.MemberForm;
import com.wangkang.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private  StringRedisTemplate redisTemplate;

    public MemberServiceImpl(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 会员登录
     *
     * @param member 会员表单
     * @return token
     */
    @Override
    public String login(Member member) {
        //获取用户输入的手机和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password)){
            throw GuliException.from(ResultCode.LOGIN_ERROR);
        }


        //判断手机号是否正确
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        //根据ucenter_member表中的mobile字段，作为查询条件，通过
        // selectOne方法去查询出Member会员对象。
        wrapper.eq(Member::getMobile,mobile);
        //wrapper.eq("mobile",mobile);
        Member mobileMember = baseMapper.selectOne(wrapper);
        //Member mobileMember = this.getOne(wrapper);

        //判断对象是否为空
        if(mobileMember==null){//没有这个手机号(根据输入的这个手机号查不到Member对象)

         throw  GuliException.from(ResultCode.LOGIN_ERROR); //登录失败

        }


        //判断密码
        //因为存储到数据库密码肯定加密的
        //把用户输入的密码进行加密，再和数据库中该手机号对应的Member对象中的密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw GuliException.from(ResultCode.LOGIN_ERROR);

        }



//        if(password.equalsIgnoreCase(DigestUtils.md5DigestAsHex(mobileMember.getPassword().getBytes(StandardCharsets.UTF_8)))){
//            throw GuliException.from(ResultCode.LOGIN_ERROR);
//        }

        //判断用户是否禁用
        if (mobileMember.getIsDisabled()){
            throw GuliException.from(ResultCode.LOGIN_ERROR);
        }

        //登录成功
        //生成token字符串，使用jwt字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    /**
     * 会员注册
     *
     * @param memberForm 会员表单
     * Member中没有验证码字段，所以传参只能传MemberForm
     */
    @Override
    public void register(MemberForm memberForm) {
        //获取注册的数据

        String mobile = memberForm.getMobile();
        String password = memberForm.getPassword();
        String code = memberForm.getCode();
        String nickname = memberForm.getNickname();

        //非空判断
        if (   StringUtils.isBlank(mobile)
                || StringUtils.isBlank(password)
                || StringUtils.isBlank(code)
                || StringUtils.isBlank(nickname)
        ){
            throw GuliException.from(ResultCode.REGISTER_ERROR);
        }

        //判断验证码
        //根据手机号获取redis验证码,需要用到msm中的功能，
        //在msm中向redis中通过set方法放入验证码的前提下，
        //且在ucenter模块中yaml文件中配置的redis与msm中yaml文件中配置的redis相同时,进行此步骤。
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //如果用户填入的验证码与redis中的验证码不一致
        if (!code.equalsIgnoreCase(redisCode)){
            throw GuliException.from(ResultCode.REGISTER_ERROR);
        }

        //判断手机号是否重复，表里面是否存在相同手机号不进行添加
        LambdaQueryWrapper<Member> memberQuery = new LambdaQueryWrapper<>();
        memberQuery.eq(Member::getMobile,mobile);

        int count= baseMapper.selectCount(memberQuery);
        //int count = this.count(memberQuery);
        if (count > 0){
            throw GuliException.from(ResultCode.REGISTER_ERROR);
        }

        //数据添加到数据库
        Member member = new Member();
        BeanUtils.copyProperties(memberForm,member);
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://wk-1010.oss-cn-hangzhou.aliyuncs.com/2022/04/27/ea3a159d059b47dbba6613fb7e20d6c7file.png");
        //密码需要做加密处理
        member.setPassword(MD5.encrypt(password));
        //member.setPassword(DigestUtils.md5DigestAsHex(member.getPassword().getBytes(StandardCharsets.UTF_8)));
        //this.save(member);
        baseMapper.insert(member);
    }

    /**
     * 根据openid 获取用户信息
     *
     * @param openid
     * @return 会员
     */
    @Override
    public Member getMemberByOpenid(String openid) {

        LambdaQueryWrapper<Member> memberQuery = new LambdaQueryWrapper<>();
        memberQuery.eq(Member::getOpenid,openid);
        Member member = this.getOne(memberQuery);


        return member;
    }

    /**
     * 查询指定day的注册人数
     *
     * @param day
     * @return 注册人数
     */
    @Override
    public Integer countRegisterDay(String day) {
        return this.baseMapper.countRegisterDay(day);
    }
}